package com.egreen.alipay.controller;

import com.alipay.api.AlipayApiException;
import com.egreen.alipay.service.AlipayService;

import com.egreen.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author volcan
 * @since 2022-07-22
 */
@Slf4j
@RestController
@Api(tags = "支付宝相关模块")
@RequestMapping("/Alipay")
public class AlipayController {

    @Autowired
    AlipayService alipayService;

    @GetMapping("/getAlipayUserInfo")
    @ResponseBody
    @ApiOperation("获取支付宝会员信息")
    public Result getAlipayUserInfo(HttpServletRequest request) throws AlipayApiException {
        log.info("进入获取支付宝用户信息接口");
        return alipayService.getAlipayUserInfo(request);

    }

    @GetMapping("/bindAlipayUserInfo")
    @ResponseBody
    @ApiOperation("绑定支付宝会员信息")
    public Result bindAlipayUserInfo(HttpServletRequest request){
        log.info("进入绑定支付宝用户信息接口");
        return alipayService.bindAlipayUserInfo(request);

    }

    @GetMapping("/AlipayTransfer")
    @ResponseBody
    @ApiOperation("转账")
    public Result getAlipayBalance(HttpServletRequest request) throws AlipayApiException {
        String transfer = request.getParameter("transfer");
        int id = Integer.parseInt(request.getParameter("id"));
        return alipayService.transfer(transfer,id);
    }

    @GetMapping("/recharge")
    @ResponseBody
    @ApiOperation("充值")
    public Result recharge(BigDecimal money, HttpServletRequest request, HttpServletResponse httpResponse) throws AlipayApiException {
        int id = Integer.parseInt(request.getParameter("id"));
        return alipayService.recharge(money,id,httpResponse);
    }

    @PostMapping("/AlipayNotify")
    @ResponseBody
    @ApiOperation("支付宝异步通知接口")
    public String AlipayNotify(HttpServletRequest request){
        log.info("进入异步接口");
        return alipayService.notify(request);
    }

}


